import i18next from 'i18next'
import i18nextMiddleware from 'i18next-http-middleware'
import path from 'path'

i18next.use(i18nextMiddleware.LanguageDetector).init({
  fallbackLng: 'zh',
  preload: ['zh', 'en'],
  ns: ['common', 'validation', 'error'],
  defaultNS: 'common',
  backend: {
    loadPath: path.join(__dirname, '../locales/{{lng}}/{{ns}}.json'),
  },
  detection: {
    order: ['header', 'cookie'],
    caches: ['cookie'],
  },
})

export default i18nextMiddleware.handle(i18next)
